<?php

class IndexAction extends Action {
    public function _initialize(){
        header("Connection:Keep-Alive");
        header("Content-Type:text/html;charset=utf-8");
        header("X-Powered-By:kglife.com");
    }
    public function _empty() {
        $this->redirect('Index');
    }

    public function index($message=false) {
        if ($message)
            $this->assign('message', $message);
        if ($this->isAjax()) {
            $this->display('ajax_step1');
        } else {
            $this->assign('hotsearch', array('功夫熊猫2','连衣裙','泳装','时尚女包','雪纺','女装', '魅族M9', 'iphone'));
            $this->display('index');
        }
    }

    public function search() {
        if (!isset($_REQUEST['type'])) {
            $this->redirect('Index');
        }
        switch ($_REQUEST['type']) {
            case '1':
                $itemid = doubleval($_REQUEST['itemid']);
                if (!($res = S('item_' . $itemid))) {
                    import('@.Com.Taobao');
                    $taoAjax = new Taobao();
                    $res = $taoAjax->getItem($itemid);
                    $res ? $res['obj_url'] = 'http://item.taobao.com/item.htm?id=' . $itemid : '';
                    S('item_' . $itemid, $res, C('TAOBAO_ITEM_CACHE'));   //设置过期时间
                    if ($res) {
                        $data = F('items' . date('Ymd'));
                        $data["{$res['num_iid']}"] = "折扣{$res['commission_rate']}%,{$res['title']}";
                        F('items' . date('Ymd'), $data);
                    }else{
                        C('HTML_CACHE_ON',false);
                    }
                }
                break;
            case '2':
                $shopid = doubleval($_REQUEST['shopid']);
                if (!($res = S('shop_' . $shopid))) {
                    import('@.Com.Taobao');
                    $taoAjax = new Taobao();
                    $res = $taoAjax->getShop($shopid);
                    $res ? $res['obj_url'] = 'http://shop' . $shopid . '.taobao.com' : '';
                    S('shop_' . $shopid, C('TAOBAO_ITEM_CACHE'));
                }
                break;
            case '3':
                break;
        }
        unset($taoAjax);
        if (!$res) {
            $this->index('该商品没有隐藏折扣');
        } else {
            $this->assign('item_title', "隐藏淘宝折扣{$res['commission_rate']}%,{$res['title']}");
            $this->assign('item_keywords', "隐藏折扣{$res['commission_rate']}%,{$res['title']}");
            $this->assign('item_desc', '该商品[' . $res['title'] . ']含隐藏折扣' . $res['commission_rate'] . '%，通过寻利淘购买更省钱。');
            $this->assign('res', $res);
            if ($this->isAjax()) {
                $this->display('ajax_step2');
            } else {
                $this->display('step2');
            }
        }
    }

    /**
     * 搜索宝贝
     */
    public function searchitem() {
        $keyword = urldecode($_REQUEST['keyword']);
        $page = intval($_REQUEST['page']) > 0 ? intval($_REQUEST['page']) : 1;
        $order = intval($_REQUEST['o']);
        $commission = intval($_REQUEST['c']);
        import('@.Com.Taobao');
        $taoTop = new Taobao();
        $res = $taoTop->searchItem($keyword, $page, $order, $commission);
        $this->assign('list', $res);
        unset($res,$taoTop);
        $this->assign('item_title', $keyword . '_淘宝折扣_寻利淘');
        $this->assign('item_keywords', $keyword.',淘宝折扣');
        $this->assign('item_desc', '挖掘淘宝' . $keyword . '的隐藏折扣，购物更省钱，打折更省心。');
        $this->display('search');
        if ($page == 1) {
            $data = F('searchitems' . date('Ymd'));
            $data["{$keyword}"] = intval($data[$keyword]) + 1;
            F('searchitems' . date('Ymd'), $data);
        }
    }

    /**
     * 每日热门搜索
     */
    public function searchList() {
        $now = $_GET['date'] ? $_GET['date'] : date('Ymd');
        $this->assign('item_title', $now . ' - 热门搜索');
        $this->assign('item_keywords', '热门查询');
        $this->assign('item_desc', '热门查询列表页面');
        $this->assign('list', F('searchitems' . $_GET['date']));
        $this->display('searchList');
    }

    /**
     * 每日搜索引导
     */
    public function searchindex() {
        $data = array();
        for ($now = date('Ymd'), $i = 1; $now >= 20110524; ++$i) {
            $data[] = $now;
            $now = date('Ymd', time() - 86400 * $i);
        }
        $this->assign('item_title', '每日热门搜索_寻利淘');
        $this->assign('item_keywords', '每日热么搜索,寻利淘');
        $this->assign('item_desc', '寻利淘每日热门搜索索引');
        $this->assign('list', $data);
        $this->display('searchindex');
    }

    public function createurl() {
        if (isset($_REQUEST['type'])) {
            import('@.Com.Taobao');
            $taoAjax = new Taobao();
        } else {
            $this->redirect('Index');
        }
        $nick = urldecode($_REQUEST['nick']);
        switch ($_REQUEST['type']) {
            case '1':
                $data = $taoAjax->getItem(doubleval($_REQUEST['itemid']), $nick);
                break;
            case '2':
                $data = $taoAjax->getShop(doubleval($_REQUEST['shopid']), $nick);
                break;
        }
        if ($_REQUEST['callback']) {
            echo $_REQUEST['callback'], '(', json_encode($data['click_url']), ');';
        } else {
            echo json_encode($data['click_url']);
        }
    }

    /**
     * 热门查询
     */
    public function itemsList() {
        $now = $_GET['date'] ? $_GET['date'] : date('Ymd');
        $this->assign('item_title', getDateV($now) . '_热门查询_寻利淘_淘宝折扣');
        $this->assign('item_keywords', getDateV($now).',每日热么搜索,寻利淘');
        $this->assign('item_desc', '挖掘淘宝' . $keyword . '的隐藏折扣，购物更省钱，打折更省心。');
        $this->assign('list', F('items' . $_GET['date']));
        $this->display('itemsList');
    }

    /**
     * 每日热门查询引导页
     */
    public function itemindex() {
        $data = array();
        for ($now = date('Ymd'), $i = 1; $now >= 20110524; ++$i) {
            $data[] = $now;
            $now = date('Ymd', time() - 86400 * $i);
        }
        $this->assign('item_title', '每日热门查询_寻利淘_淘宝折扣');
        $this->assign('item_keywords','热么查询,寻利淘');
        $this->assign('item_desc', '寻利淘每日热门查询索引,淘宝折扣,尽在寻利淘');
        $this->assign('list', $data);
        $this->display('itemindex');
    }

    /**
     * 获取连接
     * @todo 还没有完成
     */
    public function getTaoUrl() {
        $url = $_GET['url'];
        $url = 'http://' . $url;
        $opts = array('http' => array('method' => 'GET', 'timeout' => 20));
        $opts = stream_context_create($opts);
        for ($i = 0; $i < 4; ++$i) {
            $data = @file_get_contents($url, 0, $opts, 0, 25 * 1024);
            echo $i;
            if ($data)
                break;
        }
        dump($data);
    }

    //最多查询商品
    public function topList() {
        
    }

    //
    public function topItem() {
        
    }

}

?>